<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\Assets as AssetsModel;
use app\common\Logs;
use think\Db;
class Assets extends BaseController{    
    public function __construct(AssetsModel $model){
        parent::__construct();
        $this->model = $model;
    }
    public function index(){        
        $params = $this->request->param();  
        if(empty($params['type'])){
            return '缺少type参数';
        }
        if(!empty($params['userId'])){
            $params['operator_id'] = $params['userId'];
        }     
        $data = $this->model->Lists($params);
        return $data;
    }
    public function all(){        
        $params = $this->request->param();          
        $data = $this->model->select();
        return $data;
    }
    //查询指定数据
    public function read(){        
        $params = $this->request->param();        
        if(empty($params['sn'])){
            return '缺少sn参数';
        }      
        if(empty($params['type'])){
            return '缺少type参数';
        }  
        $type = $params['type'];
        $data = $this->model->with([
            'assetsType'=>'assetsType',
            'assetsModel'=>'assetsModel',
            'assetsUnit'=>'assetsUnit', 
            'operator'=>'operator'
        ])->where('sn',$params['sn'])->order('id', 'desc')->select();
        
        return $data ;
    }
    //添加
    public function create(){        
        $param = $this->request->param();                
        $params = json_decode($param['params'],true);        
        $operator_id = $params['operator_id'];
        //type,1：团队，2：散拼，3：项目
        if(empty($params['type'])){
            return '缺少type参数';
        }  
        $params['sn'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $data = $this->model->allowField(true)->isUpdate(false)->data($params)->save();
        if($data){            
            Logs::add("新增固定资产记录",$operator_id,$this->model->sn, null,json_encode($this->model)); 
        }
        return $this->model;
    }
    //编辑
    public function update(){        
        $param = $this->request->param();         
        $params = json_decode($param['params'],true);        
        $operator_id = $params['operator_id'];
        $old = $this->model->where('id',$params['id'])->find();        
        $data = $this->model->allowField(true)->isUpdate(true)->save($params,['id',$params['id']]);
        if($data){            
            Logs::add("修改固定资产记录",$operator_id,$old['sn'], json_encode($old),json_encode($this->model)); 
        }
        return $this->model;
    }
    //删除
    public function delete(){
        $params = $this->request->param();        
        $old = $this->model->where('id',$params['id'])->find();
        $data = $this->model->allowField(true)->where('id',$params['id'])->delete();
        if($data){            
            Logs::add("删除固定资产记录",$params['operator_id'],$old['sn'], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE)); 
        }
        return $data;
    }

//导出excel
public function export_excel(){     
        $title = array('预订日期','出发日期','回程日期','编号','所属','部门','客户','客人','供应商','订单内容','现付价','销售价','销售金额','已收款','未收款','成本金额','已付款','未付款','毛利','业务员','操作员','业务提成','操作提成');
        include(EXTEND_PATH.'phpexcel/PHPExcel.php'); 
        $obj = new \PHPExcel();      
        $obj->getActiveSheet()->getColumnDimension('A')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('B')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('C')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('D')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('F')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('G')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('H')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('I')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('J')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('N')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('O')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('P')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('Q')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('R')->setWidth(18);
        //横向单元格标识      
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');  
        $obj->getActiveSheet(0)->setTitle('sheet名称');   //设置sheet名称      
        $_row = 1;   //设置纵向单元格标识      
        if($title){      
            $_cnt = count($title);    
            $i = 0;      
            foreach($title AS $v){   //设置列标题      
                $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);      
                $i++;      
            }      
            $_row++;      
        }      

        $params = $this->request->param();
        //type,1：团队，2：散拼，3：项目
        if(empty($params['type'])){
            return '缺少type参数';
        }
        if(!empty($params['userId'])){
            $params['operator_id'] = $params['userId'];
        }     
        $data = $this->model->singleExcel($params);
        //填写数据      
        if($data){      
            $i = 0;      
            foreach($data AS $_v){         
                $obj->getActiveSheet(0)->setCellValueExplicit($cellName[0] . ($i+$_row), $_v['booking_date'],\PHPExcel_Cell_DataType::TYPE_STRING);       
                $obj->getActiveSheet(0)->setCellValueExplicit($cellName[1] . ($i+$_row), $_v['start_date'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $obj->getActiveSheet(0)->setCellValue($cellName[2] . ($i+$_row), $_v['end_date'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $obj->getActiveSheet(0)->setCellValue($cellName[3] . ($i+$_row), $_v['sn']);
                $obj->getActiveSheet(0)->setCellValue($cellName[4] . ($i+$_row), $_v['category_id']);
                $obj->getActiveSheet(0)->setCellValue($cellName[5] . ($i+$_row), $_v['branch_id']);
                $obj->getActiveSheet(0)->setCellValue($cellName[6] . ($i+$_row), $_v['client_id']);
                $obj->getActiveSheet(0)->setCellValue($cellName[7] . ($i+$_row), $_v['guest_id']);
                $obj->getActiveSheet(0)->setCellValue($cellName[8] . ($i+$_row), $_v['supplier_id']);
                $obj->getActiveSheet(0)->setCellValueExplicit($cellName[9] . ($i+$_row), $_v['product_name'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $obj->getActiveSheet(0)->setCellValue($cellName[10] . ($i+$_row), $_v['pay_price']);    
                $obj->getActiveSheet(0)->setCellValue($cellName[11] . ($i+$_row), $_v['sell_price']);    
                $obj->getActiveSheet(0)->setCellValue($cellName[12] . ($i+$_row), $_v['receivable']);    
                $obj->getActiveSheet(0)->setCellValue($cellName[13] . ($i+$_row),$_v['received']);            
                $obj->getActiveSheet(0)->setCellValue($cellName[14] . ($i+$_row), $_v['receivable']-$_v['received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[15] . ($i+$_row), $_v['payable']);
                $obj->getActiveSheet(0)->setCellValue($cellName[16] . ($i+$_row),$_v['pay']);
                $obj->getActiveSheet(0)->setCellValue($cellName[17] . ($i+$_row), $_v['payable']-$_v['pay']);
                $obj->getActiveSheet(0)->setCellValue($cellName[18] . ($i+$_row), $_v['receivable']-$_v['payable']);              
                $obj->getActiveSheet(0)->setCellValue($cellName[19] . ($i+$_row), $_v['salesman_id']);         
                $obj->getActiveSheet(0)->setCellValue($cellName[20] . ($i+$_row),$_v['operator_id']);       
                $obj->getActiveSheet(0)->setCellValue($cellName[21] . ($i+$_row), ($_v['receivable']-$_v['payable'])*0.1);
                $obj->getActiveSheet(0)->setCellValue($cellName[22] . ($i+$_row), ($_v['receivable']-$_v['payable'])*0.05);                     
                $i++;      
            }      
        }  
        //文件名处理      
        
        $fileName = uniqid(time(),true);      
        
        $objWrite = \PHPExcel_IOFactory::createWriter($obj, 'Excel2007');  
        $isDown=true;
        if($isDown){   //网页下载      
            header('pragma:public');      
            header("Content-Disposition:attachment;filename=$fileName.xls");      
            $objWrite->save('php://output');exit;      
        }      
        $_fileName = iconv("utf-8", "gb2312", $fileName);   //转码      
        $_savePath = $savePath.$_fileName.'.xlsx';      
        $objWrite->save($_savePath);  
        return $savePath.$fileName.'.xlsx';      
    }    

    public function csv(){        
        $params = $this->request->param();        
        if(empty($params['type'])){
            return '缺少type参数';
        }
        if(!empty($params['userId'])){
            $params['operator_id'] = $params['userId'];
        }     
        $data = $this->model->allLists($params);
        return $data;
    }
}